import Vue from 'vue'
import Router from 'vue-router'
import layout from '@/pages/layout/layout'
import login from '@/pages/login/login'
import home from '@/pages/home/home'
import deviceList from '@/pages/device/deviceList'
import edit from '@/pages/device/edit'
import echarts from '@/pages/echarts/echarts'
import {getCookie} from '../until/until'

Vue.use(Router)

 const router = new Router({
	mode: 'history',
  routes: [
    {
      path: '/',
      name: 'login',
      component: login,
	  hidden: true
    },
	{
	  path: '/login',
	  name: 'login',
	  component: login,
	  hidden: true
	},
	{
	  path: '/',
	  name: '',
	  component: layout,
	  iconCls: 'el-icon-tickets',
	  leaf: true,
	  children: [
		  {
			  path: '/home',
			  component: echarts,
			  name: '首页',
			  meta: {
			    requireAuth: true,  // 添加该字段，表示进入这个路由是需要登录的
			  },
		  }
	  ]
	},
	{
	  path: '/',
	  name: '设备管理',
	  component: layout,
	  iconCls: 'el-icon-tickets',
	  leaf: false,
	  children: [
	  		  {
	  			  path: '/deviceList',
	  			  component: deviceList,
	  			  name: '设备列表',
				  meta: {
				    requireAuth: true,  // 添加该字段，表示进入这个路由是需要登录的
				  },
	  		  },{
	  			  path: '/edit',
	  			  component: edit,
	  			  name: 'edit',
				  meta: {
				    requireAuth: true,  // 添加该字段，表示进入这个路由是需要登录的
				  },
	  		  }
	  ]
	},
	{
	path:'/*',
	redirect:'/home',
	hidden:true
    },
	
  ],
	
})

router.beforeEach((to,from,next) => {
	console.log(to)
	 if (to.matched.some(res => res.meta.requireAuth)) {// 判断是否需要登录权限
		// getCookie('session')
		
		if(localStorage.getItem('token')){
			next()
		}else{
			next({
				path: '/login',
				query: {redirect: to.fullPath}
			})
			console.log(to.fullPath)
		}
	}else{
		next()
	}
})

export default router